package cn.itsource.org.config;

import com.github.pagehelper.autoconfigure.PageHelperAutoConfiguration;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.List;

/**
 * @Class_Name: MybatisInterceptorConfig
 * @author: maizi
 * @Description:
 * @Date: 16:18 2022/3/1
 */
@Component
@Configuration
@AutoConfigureBefore({PageHelperAutoConfiguration.class})
@Slf4j
public class MybatisInterceptorConfig {

    @Autowired
    private List<SqlSessionFactory> sqlSessionFactoryList;


    /**
     * 参考
     * com.github.pagehelper.PageInterceptor
     * 将我们自定义的mybatis拦截器注入配置中
     */
    @PostConstruct
    public void addMybatisInterceptor() {
        MybatisInterceptor interceptor = new MybatisInterceptor();
        sqlSessionFactoryList.forEach (sqlSessionFactory -> sqlSessionFactory.getConfiguration().addInterceptor(interceptor));

        //sqlSessionFactory.getConfiguration().addInterceptor(interceptor);
    }
}
